package com.epalmpay.controller.channel.agent;

import javax.annotation.Resource;

import com.epalmpay.dto.member.MemberInDTO;
import com.epalmpay.dto.member.MemberOutDTO;
import com.epalmpay.dto.merchantmanage.GroupOutDTO;
import com.epalmpay.entity.Member;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.epalmpay.commom.BaseController;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.status.Rescode;
import com.epalmpay.dto.system.BaseOutDTO;
import com.epalmpay.dto.system.PageDTO;
import com.epalmpay.dto.activity.AgentRateInDTO;
import com.epalmpay.dto.activity.AgentRateOutDTO;
import com.epalmpay.dto.agent.AgentApproveDTO;
import com.epalmpay.dto.agent.AgentFinanceInfoOut;
import com.epalmpay.dto.agent.AgentInDTO;
import com.epalmpay.dto.agent.AgentInfoInDTO;
import com.epalmpay.dto.agent.AgentInfoOutDTO;
import com.epalmpay.dto.agent.AgentListOutDTO;
import com.epalmpay.dto.agent.AgentMoreInfoOutDTO;
import com.epalmpay.dto.agent.SingleAgentRateInDTO;
import com.epalmpay.dto.agent.SubAgentSetInfoOutDTO;
import com.epalmpay.dto.member.BalanceInDTO;
import com.epalmpay.dto.prepurchase.PrePurAgentInDTO;
import com.epalmpay.entity.Agent;
import com.epalmpay.service.agent.IAgentService;

import java.util.List;
import java.util.Map;


@Controller
@RequestMapping(value="/channel/agent")
public class AgentController extends BaseController{

	
	
	/**
	 * 
	 */
	@Resource
	IAgentService agentservice;
	
	@RequestMapping(value="/agentList")
	@ResponseBody
	public BaseOutDTO  agentList(AgentInDTO agentInDTO,PageDTO pageDTO){
		BaseOutDTO baseOutDTO = new BaseOutDTO();
		try {		
			AgentListOutDTO agentListOutDTO = agentservice.agentList(agentInDTO,pageDTO);
			if(agentListOutDTO!=null){
				agentListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
				return agentListOutDTO;
			}else{
				baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理结果为空");
			}	
		} catch (ApplicationException ae) {
			baseOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		catch (Exception e) {
			baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return baseOutDTO;
	}
	
	
	/**
	 * 大商户查询代理商列表
	 * @param agentInDTO
	 * @param pageDTO
	 * @return
	 */
	@RequestMapping(value="/grouplist")
	@ResponseBody
	public BaseOutDTO  grouplist(AgentInDTO agentInDTO,PageDTO pageDTO){
		BaseOutDTO baseOutDTO = new BaseOutDTO();
		try {		
			AgentListOutDTO agentListOutDTO = agentservice.groupAgentlist(agentInDTO,pageDTO);
			if(agentListOutDTO!=null){
				agentListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
				return agentListOutDTO;
			}else{
				baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理结果为空");
			}	
		} catch (ApplicationException ae) {
			baseOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		catch (Exception e) {
			baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return baseOutDTO;
	}
	
	
	@RequestMapping(value="/saveAgent")
	@ResponseBody
	public BaseOutDTO  agenFinishAdd(AgentInfoInDTO agentInfoInDTO){
		BaseOutDTO baseOutDTO = new BaseOutDTO();
		try {
			
			Boolean flag = agentservice.addAgentInfo(agentInfoInDTO,ROOT_HOME_URL);
			if(flag){
				baseOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
				return baseOutDTO;
			}else{
				baseOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			}

		} catch (ApplicationException ae) {
			baseOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		catch (Exception e) {
			baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return baseOutDTO;
	}
	
	
	/**
	 * 代理商注册
	 * @param agentInfoInDTO
	 * @return
	 */
	@RequestMapping(value="/saveAgentVerify")
	@ResponseBody
	public BaseOutDTO  saveAgentVerify(AgentInfoInDTO agentInfoInDTO){
		BaseOutDTO baseOutDTO = new BaseOutDTO();
		try {
			
			Boolean flag = agentservice.addAgentInfoVerify(agentInfoInDTO);
			if(flag!=null){
				baseOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
				return baseOutDTO;
			}else{
				baseOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			}
		} catch (ApplicationException ae) {
			baseOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		catch (Exception e) {
			baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return baseOutDTO;
	}
	
	/**
	 * 代理商修改
	 * @param agentInfoInDTO
	 * @return
	 */
	@RequestMapping(value="/updateAgent")
	@ResponseBody
	public BaseOutDTO  agentFinishUpdate(AgentInfoInDTO agentInfoInDTO){
		BaseOutDTO baseOutDTO = new BaseOutDTO();
		try {
			Boolean flag = agentservice.updateAgentInfo(agentInfoInDTO);
			if(flag){
				baseOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
				return baseOutDTO;
			}else{
				baseOutDTO.setErrorCode(Rescode.SUCCESS, "处理失败");
			}
		} catch (ApplicationException ae) {
			baseOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		catch (Exception e) {
			baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, e.getMessage());
			e.printStackTrace();
		}
		return baseOutDTO;
	}
	
	
	/**
	 * 代理商审核修改
	 * @param agentInfoInDTO
	 * @return
	 */
	@RequestMapping(value="/updateAgentVerify")
	@ResponseBody
	public BaseOutDTO  agentVerifyUpdate(AgentInfoInDTO agentInfoInDTO){
		BaseOutDTO baseOutDTO = new BaseOutDTO();
		try {
			Boolean flag = agentservice.updateAgentVerifyInfo(agentInfoInDTO);
			if(flag){
				baseOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
				return baseOutDTO;
			}else{
				baseOutDTO.setErrorCode(Rescode.SUCCESS, "处理失败");
			}
		} catch (ApplicationException ae) {
			baseOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		catch (Exception e) {
			baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, e.getMessage());
			e.printStackTrace();
		}
		return baseOutDTO;
	}
	

	/**
	 * 查看代理商详情 根据ID 
	 * @param agentInDTO
	 * @return
	 */
	@RequestMapping(value="/findAgentView")
	@ResponseBody
	public BaseOutDTO  findAgentById(AgentInDTO agentInDTO){
		BaseOutDTO baseOutDTO = new BaseOutDTO();
		try {   
			Agent agent=new Agent();
			agent.setId(agentInDTO.getId());
			AgentInfoOutDTO agentInfoOut=agentservice.findAgentView(agent);
			if(agentInfoOut!=null){
				agentInfoOut.setErrorCode(Rescode.SUCCESS, "查询成功");
				return agentInfoOut;
			}else{
				baseOutDTO.setErrorCode("-1", "未查询到记录");
			}
		} catch (ApplicationException e) { 
			System.out.println(e.getMessage()); 
			baseOutDTO.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		
		return baseOutDTO;
	}
	

	/**
	 * 代理商查看代理商详情 带余额查询
	 * @param agentInDTO
	 * @return
	 */
	@RequestMapping(value="/agentMoreInfo")
	@ResponseBody
	public BaseOutDTO  agentMoreInfoOutDTO(AgentInDTO agentInDTO){
		BaseOutDTO baseOutDTO = new BaseOutDTO();
		try {   
			AgentMoreInfoOutDTO agentMoreInfoOutDTO=agentservice.findAgentInfoById(agentInDTO.getId());
			if(agentMoreInfoOutDTO!=null){
				agentMoreInfoOutDTO.setErrorCode(Rescode.SUCCESS, "查询成功");
				return agentMoreInfoOutDTO;
			}else{
				baseOutDTO.setErrorCode("-1", "未查询到记录");
			}
		} catch (ApplicationException e) { 
			logBefore(logger, e.getErrMsg());
			baseOutDTO.setErrorCode(e.getErrCode(), e.getErrMsg());
			e.printStackTrace();
		}catch (Exception e) {
			logger.equals(e);
			baseOutDTO.setErrorCode("-1", "系統异常");
			e.printStackTrace();
		}
		return baseOutDTO;
	}



	/**
	 * 查询账户代理商余额
	 * @param agentInDTO
	 * @return
	 */
	@RequestMapping(value="/queryAgentAmount")
	@ResponseBody
	public MemberOutDTO queryAgentAmount(AgentInDTO agentInDTO){
		MemberOutDTO memberOutDTO=new MemberOutDTO();
		try {
			memberOutDTO =agentservice.queryAgentAmount(agentInDTO);
			memberOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return memberOutDTO;
		}catch (ApplicationException ae) {
			logger.error(ae);
			memberOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, ae.getMessage());
		} catch (Exception e) {
			memberOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return memberOutDTO;
	}

    @RequestMapping(value="/queryGroupInfoByGroupId")
    @ResponseBody
    public GroupOutDTO queryGroupInfoByGroupId(AgentInDTO agentInDTO){
        GroupOutDTO groupOutDTO=new GroupOutDTO();
        try {
            groupOutDTO =agentservice.queryGroupInfoByGroupId(agentInDTO);
            groupOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
            return groupOutDTO;
        }catch (ApplicationException ae) {
            logger.error(ae);
            groupOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, ae.getMessage());
        } catch (Exception e) {
            groupOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }
        return groupOutDTO;
    }


    /**
     * 代理商钱包修改余额
     * @param balanceInDTO
     * @return
     */

    @RequestMapping(value="/addAgentBalance")
    @ResponseBody
    public MemberOutDTO addAgentBalance(BalanceInDTO balanceInDTO) {
        MemberOutDTO groupOutDTO = new MemberOutDTO();
        try {
            groupOutDTO =agentservice.addAgentBalance(balanceInDTO,ROOT_HOME_URL);
            return groupOutDTO;
        }catch (ApplicationException ae) {
            logger.error(ae);
            groupOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, ae.getMessage());
        }catch (Exception e) {
            groupOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }
        return groupOutDTO;

    }

	
	/**
	 * 分润设置详情
	 * @param agentRateInDTO
	 * @return
	 */
	@RequestMapping(value="/subSetInfo")
	@ResponseBody
	public BaseOutDTO  subSetInfo(AgentRateInDTO agentRateInDTO){
		BaseOutDTO baseOutDTO = new BaseOutDTO();
		try {   
			AgentRateOutDTO agentRateOutDTO=agentservice.findAgentInfo(agentRateInDTO.getAgentId());
			if(agentRateOutDTO!=null){
				return agentRateOutDTO;
			}else{
				baseOutDTO.setErrorCode("-1", "未查询到记录");
			}
		} catch (ApplicationException e) { 
			logBefore(logger, e.getErrMsg());
			baseOutDTO.setErrorCode(e.getErrCode(), e.getErrMsg());
			e.printStackTrace();
		}catch (Exception e) {
			logger.equals(e);
			baseOutDTO.setErrorCode("-1", "系統异常");
			e.printStackTrace();
		}
		return baseOutDTO;
	}
	
	

	/**
	 * 分润设置保存
	 * @param agentRateInDTO
	 * @return
	 */
	@RequestMapping(value="/saveSubSet")
	@ResponseBody
	public BaseOutDTO  saveSubSet(AgentRateInDTO agentRateInDTO){
		BaseOutDTO baseOutDTO = new BaseOutDTO();
		try {   
			Boolean flag=agentservice.saveSubSet(agentRateInDTO);
			if(flag){
				baseOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
				return baseOutDTO;
			}else{
				baseOutDTO.setErrorCode("-1", "未查询到记录");
			}
		} catch (ApplicationException e) { 
			logBefore(logger, e.getErrMsg());
			baseOutDTO.setErrorCode(e.getErrCode(), e.getErrMsg());
			e.printStackTrace();
		}catch (Exception e) {
			logger.equals(e);
			baseOutDTO.setErrorCode("-1", "系統异常");
			e.printStackTrace();
		}
		return baseOutDTO;
	}
	
	/**
	 *  单个设置 代理商 分润设置保存
	 * @param singleAgentRateInDTO
	 * @return
	 */
	@RequestMapping(value="/doUpdateAgentProfit")
	@ResponseBody
	public BaseOutDTO  doUpdateAgentProfit(SingleAgentRateInDTO singleAgentRateInDTO){
		BaseOutDTO baseOutDTO = new BaseOutDTO();
		try {   
			Boolean flag=agentservice.doUpdateAgentProfit(singleAgentRateInDTO);
			if(flag){
				baseOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
				return baseOutDTO;
			}else{
				baseOutDTO.setErrorCode("-1", "未查询到记录");
			}
		} catch (ApplicationException e) { 
			logBefore(logger, e.getErrMsg());
			baseOutDTO.setErrorCode(e.getErrCode(), e.getErrMsg());
			e.printStackTrace();
		}catch (Exception e) {
			logger.equals(e);
			baseOutDTO.setErrorCode("-1", "系統异常");
			e.printStackTrace();
		}
		return baseOutDTO;
	}
	
	/**
	 * 完成代理商的审核
	 * @param agentInfoInDTO
	 * @return
	 */
	@RequestMapping(value="/doAgentApprove")
	@ResponseBody
	public BaseOutDTO  doAgentApprove(AgentInfoInDTO agentInfoInDTO){
		BaseOutDTO baseOutDTO = new BaseOutDTO();
		try {   
			Boolean flag=agentservice.doAgentApprove(agentInfoInDTO);
			if(flag){
				baseOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
				return baseOutDTO;
			}else{
				baseOutDTO.setErrorCode("-1", "未查询到记录");
			}
		} catch (ApplicationException e) { 
			logBefore(logger, e.getErrMsg());
			baseOutDTO.setErrorCode(e.getErrCode(), e.getErrMsg());
			e.printStackTrace();
		}catch (Exception e) {
			logger.equals(e);
			baseOutDTO.setErrorCode("-1", "系統异常");
			e.printStackTrace();
		}
		return baseOutDTO;
	}
	
	
	/**
	 * 查看代理 余额信息
	 * @param agentInDTO
	 * @return
	 */
	@RequestMapping(value="/agentFinanceInfo")
	@ResponseBody
	public BaseOutDTO  agentFinanceInfo(AgentInDTO agentInDTO){
		BaseOutDTO baseOutDTO = new BaseOutDTO();
		
		try {   
			AgentFinanceInfoOut agentFinanceInfoOut=agentservice.findAgentFinanceInfo(agentInDTO.getId());
			if(agentFinanceInfoOut!=null){
				agentFinanceInfoOut.setErrorCode(Rescode.SUCCESS, "查询成功");
				return agentFinanceInfoOut;
			}else{
				baseOutDTO.setErrorCode("-1", "未查询到记录");
			}
		} catch (ApplicationException e) { 
			logBefore(logger, e.getErrMsg());
			baseOutDTO.setErrorCode(e.getErrCode(), e.getErrMsg());
			e.printStackTrace();
		}catch (Exception e) {
			logger.equals(e);
			baseOutDTO.setErrorCode("-1", "系統异常");
			e.printStackTrace();
		}
		
		return baseOutDTO;
	}
	
	
	
	/**
	 * 启用 禁用状态
	 * @param agentInDTO
	 * @return
	 */
	@RequestMapping(value="/stopAgent")
	@ResponseBody
	public BaseOutDTO  stopAgent(AgentInDTO agentInDTO){
		BaseOutDTO baseOutDTO = new BaseOutDTO();
		try {   
			BaseOutDTO baseOut=agentservice.finishStopAgent(agentInDTO.getId());
			return baseOut;
		}catch (ApplicationException e) { 
			logBefore(logger, e.getErrMsg());
			baseOutDTO.setErrorCode(e.getErrCode(), e.getErrMsg());
			e.printStackTrace();
		}catch (Exception e) {
			logger.equals(e);
			baseOutDTO.setErrorCode("-1", "系統异常");
			e.printStackTrace();
		}
		return baseOutDTO;
	}
	
	
	/**
	 * 审核代理商
	 * @param agentInDTO
	 * @return
	 */
	@RequestMapping(value="/auditAgent")
	@ResponseBody
	public BaseOutDTO  auditAgent(AgentApproveDTO agentInDTO){
		BaseOutDTO baseOutDTO = new BaseOutDTO();
		try {   
			agentservice.updateAuditAgent(agentInDTO);
			baseOutDTO.setErrorCode(Rescode.SUCCESS, "查询成功");
		}catch (ApplicationException e) { 
			logBefore(logger, e.getErrMsg());
			baseOutDTO.setErrorCode(e.getErrCode(), e.getErrMsg());
			e.printStackTrace();
		}catch (Exception e) {
			logger.equals(e);
			baseOutDTO.setErrorCode("-1", "系統异常");
			e.printStackTrace();
		}
		return baseOutDTO;
	}
	
	/**
	 * 代理商设置
	 * @param prePurAgentInDTO
	 * @return
	 */
	@RequestMapping(value="/doPrePurAgentUpdate")
	@ResponseBody
	public BaseOutDTO  doPrePurAgentUpdate(PrePurAgentInDTO prePurAgentInDTO){
		BaseOutDTO baseOutDTO = new BaseOutDTO();
		
		try {   
			boolean result=agentservice.doPrePurAgentUpdate(prePurAgentInDTO);
			if(result){
				baseOutDTO.setErrorCode(Rescode.SUCCESS, "查询成功");
			}else{
				baseOutDTO.setErrorCode("0002", "操作失败");
			}
			
		}catch (ApplicationException e) { 
			logBefore(logger, e.getErrMsg());
			baseOutDTO.setErrorCode(e.getErrCode(), e.getErrMsg());
			e.printStackTrace();
		}catch (Exception e) {
			logger.equals(e);
			baseOutDTO.setErrorCode("-1", "系統异常");
			e.printStackTrace();
		}
		
		return baseOutDTO;
	}
	
	
	
	
	/**
	 * 代理商查看费率详情
	 * @param agentRateInDTO
	 * @return
	 */
	@RequestMapping(value="/subAgentSetInfo")
	@ResponseBody
	public BaseOutDTO  subAgentSetInfo(AgentRateInDTO agentRateInDTO){
		BaseOutDTO baseOutDTO = new BaseOutDTO();
		try {   
			SubAgentSetInfoOutDTO subAgentSetInfoOutDTO=agentservice.findAgentSetInfo(agentRateInDTO.getAgentId(),agentRateInDTO.getAgentLevel());
			if(subAgentSetInfoOutDTO!=null){
				subAgentSetInfoOutDTO.setErrorCode(Rescode.SUCCESS, "查询成功");
				return subAgentSetInfoOutDTO;
			}else{
				baseOutDTO.setErrorCode("-1", "未查询到记录");
			}
		} catch (ApplicationException e) { 
			logBefore(logger, e.getErrMsg());
			baseOutDTO.setErrorCode(e.getErrCode(), e.getErrMsg());
			e.printStackTrace();
		}catch (Exception e) {
			logger.equals(e);
			baseOutDTO.setErrorCode("-1", "系統异常");
			e.printStackTrace();
		}
		return baseOutDTO;
	}
	
	/*
	* 根据大商户ID查询所有代理商
	* */
	@RequestMapping(value = "/listGroupAgent")
	@ResponseBody
	public AgentListOutDTO listGroupAgent(AgentInDTO agentInDTO) {
		return agentservice.listGroupAgent(agentInDTO);
	}

	@RequestMapping(value = "/listGroupAgent2")
	@ResponseBody
	public List<Map<String, Object>> listGroupAgent2(@RequestBody Long groupId) {
		return agentservice.listGroupAgent2(groupId);
	}
	
	/**
	 * 查询代理商所在省
	 * @return
	 */
	@RequestMapping(value = "/provincelist")
	@ResponseBody
	public AgentListOutDTO provincelist() {
		return agentservice.getProvincelist();
	}
	
	
	

	@RequestMapping(value="/addBalance")
	@ResponseBody
	public BaseOutDTO addBalance(BalanceInDTO balanceInDTO) {
		BaseOutDTO groupOutDTO = new BaseOutDTO();
		try {
			BaseOutDTO baseOutDTO=agentservice.addBalance(balanceInDTO,ROOT_HOME_URL);
			return baseOutDTO;
		}catch (ApplicationException ae) {
			logger.error(ae);
			groupOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, ae.getMessage());
		}catch (Exception e) {
			groupOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return groupOutDTO;

	}

	/**
	 * 重置支付密码
	 * @param memberInDTO
	 * @return
	 */
	@RequestMapping(value="/reSetPayPwd")
	@ResponseBody
	public BaseOutDTO reSetPayPwd(MemberInDTO memberInDTO){
		BaseOutDTO memberOutDTO=new BaseOutDTO();
		try {
			Agent member =agentservice.reSetPayPwd(memberInDTO,ROOT_HOME_URL);
//			memberOutDTO.setMember(member);
			memberOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return memberOutDTO;
		}catch (ApplicationException ae) {
			logger.error(ae);
			memberOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, ae.getMessage());
		}catch (Exception e) {
			memberOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return memberOutDTO;
	}

}



